/**
 * @author meiqin liu（叶子）
 * @version 1.0.0
 * @date 2025/4/22 8:18
 */
//启动类，主GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame {
    Scheduler scheduler = new Scheduler();
    JTextArea processArea = new JTextArea();
    JTextArea memoryArea = new JTextArea();

    public Main() {
        setTitle("操作系统调度模拟器");
        setSize(800, 600);
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel controlPanel = new JPanel();
        JButton addBtn = new JButton("添加进程");
        JButton stepBtn = new JButton("调度下一轮");

        controlPanel.add(addBtn);
        controlPanel.add(stepBtn);

        processArea.setEditable(false);
        memoryArea.setEditable(false);
        processArea.setBorder(BorderFactory.createTitledBorder("进程队列"));
        memoryArea.setBorder(BorderFactory.createTitledBorder("内存状态"));

        JPanel displayPanel = new JPanel(new GridLayout(2, 1));
        displayPanel.add(new JScrollPane(processArea));
        displayPanel.add(new JScrollPane(memoryArea));

        add(controlPanel, BorderLayout.NORTH);
        add(displayPanel, BorderLayout.CENTER);

        // 添加进程事件
        addBtn.addActionListener(e -> {
            String pid = "P" + System.currentTimeMillis() % 10000;
            int time = Integer.parseInt(JOptionPane.showInputDialog("运行时间（整数）:"));
            int priority = Integer.parseInt(JOptionPane.showInputDialog("优先权（整数）:"));
            String[] options = {"独立", "同步"};
            String type = (String) JOptionPane.showInputDialog(null, "类型:", "类型", JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
            int mem = Integer.parseInt(JOptionPane.showInputDialog("需要内存大小（KB）:"));
            PCB pcb = new PCB(pid, time, priority, type, mem);
            scheduler.addProcess(pcb);
            refresh();
        });

        // 调度事件
        stepBtn.addActionListener(e -> {
            scheduler.step();
            refresh();
        });

        setVisible(true);
    }

    private void refresh() {
        StringBuilder sb = new StringBuilder();
        sb.append("PID\t状态\t优先权\t运行时间\t属性\t内存起址\n");
        if (scheduler.running != null) {
            PCB p = scheduler.running;
            sb.append(p.pid).append("\t").append(p.status).append("\t").append(p.priority).append("\t")
                    .append(p.requireTime).append("\t").append(p.type).append("\t").append(p.startAddress).append("\n");
        }
        for (PCB p : scheduler.readyQueue) {
            sb.append(p.pid).append("\t").append(p.status).append("\t").append(p.priority).append("\t")
                    .append(p.requireTime).append("\t").append(p.type).append("\t").append(p.startAddress).append("\n");
        }
        for (PCB p : scheduler.suspended) {
            sb.append(p.pid).append("\t").append("挂起").append("\t").append(p.priority).append("\t")
                    .append(p.requireTime).append("\t").append(p.type).append("\t").append("-").append("\n");
        }

        processArea.setText(sb.toString());

        StringBuilder mem = new StringBuilder("起址\t长度\t状态\n");
        for (MemoryBLock block: MemoryManager.memory) {
            mem.append(block.toString()).append("\n");
        }
        memoryArea.setText(mem.toString());
    }

    public static void main(String[] args) {
        new Main();
    }
}

